package com.lenovo.nova.widget.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ProgressBar;

import com.lenovo.nova.util.animation.Rotate3dAnimation;
import com.lenovo.nova.utils.R;
import com.lenovo.nova.widget.baseview.CustomTextView;


public class WaitFragmentDialog extends BaseDialogFragment {

	private CustomTextView messageText;
	private ProgressBar dialog;
	private View mainView;
	
	public WaitFragmentDialog() {
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
		if(mainView == null){ 
			mainView = inflater.inflate(R.layout.layout_wait_dialog_style_1, rootView, true);
		}else{
			rootView.addView(mainView);
		}
		
		initMainView();
		return rootView;
	}

	private void initViewStyle(View rootView) {
		dialog.getLayoutParams().width = 300;
		dialog.getLayoutParams().height = 185;
		messageText.setTextSize(getActivity().getResources().getDimension(R.dimen.scenic_util_middleFont));
		messageText.setText("Loading");
	}
	
	public void setMessage(String txt){
		initMainView();
		messageText.setText(txt);
	}
	
	public void setMessageWithAnimtion(final String txt){
//		Animation animation = new RotateAnimation(0, 360f, Animation.RELATIVE_TO_SELF,0.5f,
//				Animation.RELATIVE_TO_SELF,0.5f);
//		dialog.setIndeterminate(false);
//		animation.setDuration(1000);
//		animation.setRepeatCount(-1);
//		mainView.setAnimation(animation);
//		animation.startNow();
////		animation.start();
		
		
		
		final Rotate3dAnimation animation =
                new Rotate3dAnimation(0, 180, (float) mainView.getWidth() / 2, 
                		(float) mainView.getHeight() / 2, 310.0f, true);
		
		animation.setDuration(1000);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				dialog.setVisibility(View.INVISIBLE);
				messageText.setText("");
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
//				mImgView.setVisibility(View.VISIBLE);
//				mList.setVisibility(View.GONE);
//				MyAnimation animationForImage = new MyAnimation(90f, 180f, (float) viewGroup.getWidth() / 2, (float) viewGroup.getHeight() / 2);
//				animation.setDuration(1000);
//				viewGroup.startAnimation(animationForImage);
				
				int width = mainView.getWidth();
				int height = mainView.getHeight();
				
				mainView.getLayoutParams().width = width;
				mainView.getLayoutParams().height = height;
				dialog.setVisibility(View.GONE);
				messageText.setText(txt);
			}
		});
		getView().startAnimation(animation);
	}
	
	public void initMainView(){
		if(mainView == null){
			mainView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_wait_dialog_style_1, null, false);
		}
		
		dialog = (ProgressBar) mainView.findViewById(R.id.id_prgrsbar_waitdialog_1);
		messageText	 = (CustomTextView) mainView.findViewById(R.id.id_txview_waitdialog_1);
		initViewStyle(mainView);
	}
	
	
	
	
//	class MyAnimation extends Animation {
//
//		Camera camera;
//
//		private float mCenterX;
//		private float mCenterY;
//
//		private float fromDegrees;
//
//		private float mToDegrees;
//
//		public MyAnimation(float fromDegress, float toDegress, float centerx, float centery) {
//			this.fromDegrees = fromDegress;
//			mToDegrees = toDegress;
//			mCenterX = centerx;
//			mCenterY = centery;
//		}
//
//		public void initializeInvalidateRegion(int left, int top, int right, int bottom) {
//			if (camera == null) {
//				camera = new Camera();
//			}
//		}
//
//		@Override
//		public void initialize(int width, int height, int parentWidth, int parentHeight) {
//			super.initialize(width, height, parentWidth, parentHeight);
//			if (camera == null) {
//				camera = new Camera();
//			}
//		}
//
//		@Override
//		protected void applyTransformation(float interpolatedTime, Transformation t) {
//
//			Matrix matrix = t.getMatrix();
//			float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
//
//			System.out.println("applyTransformation " + interpolatedTime + "    " + degrees);
//			camera.save();
//			camera.translate(0, 0, 1000 * interpolatedTime);
//			camera.rotateY(degrees);
//			camera.getMatrix(matrix);
//			camera.restore();
//			matrix.preTranslate(-mCenterX, -mCenterY);
//			matrix.postTranslate(mCenterX, mCenterY);
//
//		}
//
//	}
	
}
